home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / ppc.com / PPC.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1991-04-24  |  6.6 KB  |  118 lines

  1. program PPC ;
  2. {
  3.  Title        : PPC.PAS   -  Parallel Port Control
  4.  Author       : Robert Upleger
  5.  Date         : March 24, 1991
  6.  Class        : CSC 335-80  - Spring 1991 - R. A. Peloso
  7.  Objective    : To gain familiarity with the interface and control
  8.                  aspects of the IBM-PC parallel port.
  9.  Intent       : Prints a disk file using direct control of the parallel
  10.                  port hardware.  Will not use any interrupts.
  11. }
  12. uses    crt , dos , timer ;             { timer unit returns Current_Sec   }
  13.  
  14. const   end_of_file  : char = #26     ; { ASCII code for end of file       }
  15.         time_delay   : integer = 10   ; { default time delay till shutdown }
  16.  
  17. var     ch           : char           ; { one char of text file or input   }
  18.         f            : text           ; { text file                        }
  19.         filename,                       { user inputted filename           }
  20.         message      : string         ; { printer status message           }
  21.         ready        : boolean        ; { printer status vailidity         }
  22.         BaseAddress  : word           ; { port number for data function    }
  23.  
  24.       {--------------------------------------------------------------------}
  25.         procedure PrinterStatus ( var msg : string ; var test : boolean ) ;
  26.         { Assigns and updates the printer status port.  Detects and
  27.            returns ONLINE, OFFLINE, TURNED OFF or OUT OF PAPER messages.
  28.             Returns 'test' true if printer status is on-line & ready
  29.               to print.  Otherwise returns 'test' false. }
  30.         var  Status : byte ;  { status register }
  31.         begin
  32.              Status := Port [ BaseAddress + 1 ] ;     { assign status port }
  33.              test := false ;
  34.              msg  := 'Printer is On-Line.                                  ';
  35.              if Status and $90 = $80 then             { test Bit #7,#5 & #4 }
  36.                   msg  :=  'Printer is turned off or cable disconnected.   '
  37.              else if Status and $20 = $20 then               { test Bit #5  }
  38.                   msg  :=  'Printer is out of paper.                        '
  39.              else if Status and $B8 = $98 then     { test Bit #7,#5,#4 & #3 }
  40.                   test :=  true
  41.              else msg  :=  'Printer is off-line.                            '
  42.         end ;
  43.       {---------------------------------------------------------------------}
  44.         procedure Shut_Down_Printer (  y : integer ) ;
  45.         { Accepts -y- as a pointer to the screen.  Continues to display
  46.            error message while counting down to shutdown.  Program will
  47.             halt if error condition is not corrected before time_lapse. }
  48.         var  Start_Time , Elapse_Time : real ;
  49.         begin
  50.              Start_Time  := Current_Sec ;  { Current_Time from unit timer }
  51.              Elapse_Time := 0 ;
  52.              while not Ready and ( Elapse_Time < time_delay ) do begin
  53.                    GotoXY ( 1 , 1 ) ; writeln ( message ) ;
  54.                    write ( time_delay, ' seconds to shutdown.' ) ;
  55.                    Elapse_Time := Current_Sec - Start_Time ;
  56.                    write ( ' [ ', Elapse_Time:2:1 , ' ] ' ) ;  {  time   }
  57.                    PrinterStatus ( message , ready ) ; end ;
  58.              if Elapse_Time >= time_delay then begin
  59.                    GotoXY ( 1 ,  y + 3 ) ;
  60.                    writeln ( 'Printer has been halted due to time lapse.' );
  61.                    close ( f ) ; halt ;  end ;
  62.         end ;
  63.       {---------------------------------------------------------------------}
  64.         procedure Print_Char ( c : char ) ;
  65.         { Accepts and converts 'c' to ordinal value and places it in the
  66.            Data Output Port.  If the printer is not busy then the
  67.              Low Bit #0 of the Control Register is strobed from 1 to 0
  68.               telling the printer that a new byte is in the Data Port.
  69.                Otherwise program is count down to time_lapse shutdown.
  70.                 Printer Status Port messages are always displayed. }
  71.         var   x , y   : integer ;
  72.         begin
  73.              x := WhereX ; y := WhereY ;           { store screen position  }
  74.              GotoXY ( 1 , 1 )    ; ClrEol ;        { goto top of screen     }
  75.              writeln ( message ) ; ClrEol ;        { display status message }
  76.              Port [ BaseAddress ] := ord ( c ) ;{puts -c- in the Data Port  }
  77.              delay ( 100 ) ;                   { settle the status register }
  78.              PrinterStatus ( message , ready ) ;     { is the printer ready? }
  79.              if not Ready then Shut_Down_Printer ( y ) ;  { start countdown }
  80.              GotoXY ( x , y ) ;        { continue display where it left off }
  81.              Port [ BaseAddress + 2 ] :=  $0D ; {Strobe Control Bit #0 to 1 }
  82.              Port [ BaseAddress + 2 ] :=  $0C ; {Reset  Control Bit #0 to 0 }
  83.         end ;
  84.       {---------------------------------------------------------------------}
  85.  
  86. BEGIN     { main }
  87.    repeat
  88.      BaseAddress := MemW [ $0040:$8 ] ;        { port address for DOS LPT1 }
  89.      clrscr ; ch := ' ';                       { initialize char as a blank }
  90.      writeln ( 'This program delays on error conditions before halting.');
  91.      write   ( 'This delay before shutdown is ',time_delay,' seconds.');
  92.      writeln ; writeln ;
  93.      write   ( 'Input filename ( e.g. d:filename.ext ) -> ' ) ;
  94.      readln  ( filename ) ;  clrscr ;
  95.      assign  ( f , filename ) ;   reset ( f ) ;
  96.      PrinterStatus ( message , ready ) ;
  97.      if Ready then begin
  98.         { Initialize port LPT1 for printing by strobing the
  99.            Pulse Bit #2  of the Control Register from 1 to 0 with
  100.             Bit #3 set to 1 and Bit #4 set to 0 ( interrupts disabled ).}
  101.         Port [ BaseAddress + 2 ] :=  $EB ;    { Strobe Control Bit #2 to 1  }
  102.         Port [ BaseAddress + 2 ] :=  $EC ;    { Reset  Control Bit #2 to 0  }
  103.         writeln ( message ) ;                      { display printer status }
  104.         writeln; writeln;                             { two blank lines     }
  105.         read ( f , ch ) ;                             { read char from file }
  106.         while ch <> end_of_file do begin
  107.               Print_Char ( ch ) ;                      { send ch to printer }
  108.               write ( ch ) ;                       { dispaly char on screen }
  109.               read ( f , ch ) ; end ; { while }            { read next char }
  110.         writeln; writeln ;
  111.         write ( 'Enter  -Q- to quit or hit any key to continue --> ' ) ;
  112.         ch := readkey ; end { if }
  113.      else Shut_Down_Printer ( WhereY ) ; { printer not ready start shutdown }
  114.    until ( upcase ( ch ) = 'Q') or not Ready ;
  115.    close ( f ) ;
  116. end.  { main }
  117.  
  118.